<?php
/**
 * Created by PhpStorm.
 * User: xiaom
 * Date: 2016/9/23 0023
 * Time: 9:58
 */

namespace Net\Controller;

use Common\Controller\AdminbaseController;

class FailuresRepairController extends AdminbaseController
{
    protected $failures_repair_model;
    protected $failures_repair_m;
    protected $campus_model;
    protected $operator_model;
    protected $building_model;
    protected $order_model;
    protected $users_model;

    function _initialize()
    {
        parent::_initialize();
        $this->failures_repair_model = D("FailuresRepair");
        $this->failures_repair_m = M("FailuresRepair");
        $this->campus_model = D("Common/Campus");
        $this->operator_model = D("Common/Operator");
        $this->building_model = D("Common/Building");
        $this->order_model = D("Orders");
        $this->users_model = D("Users");
    }

//    首页
    function index()
    {
        $this->_list();
        $this->display();
    }

//    全部未接单
    function all_no_order()
    {
        $where['Orders.status'] = array('exp', ' is null');
        $this->_list($where);
        $this->display('index');
    }

    //全部未处理
    function all_no_handle(){
        $where['Orders.status'] = 1;
        $this->_list($where);
        $this->display('index');
    }

    //全部已处理
    function all_handled(){
        $where['Orders.status'] = 2;
        $this->_list($where);
        $this->display('index');
    }

    //个人负责区域
    function self_orders(){
        $where['FailuresRepair.building_id'] = getBuilding_id(get_current_admin_id());
        $this->_list($where);
        $this->display('index');
    }

    //个人未接单
    function self_no_order(){
        $where['Orders.status'] = array('exp', ' is null');
        $where['FailuresRepair.building_id'] = getBuilding_id(get_current_admin_id());
        $this->_list($where);
        $this->display('index');
    }

    //个人未处理
    function self_no_handle(){
        $where['Orders.status'] = 1;
        $where['Orders.user_id'] = get_current_admin_id();
        $where['FailuresRepair.building_id'] = getBuilding_id(get_current_admin_id());
        $this->_list($where);
        $this->display('index');
    }

    //个人已处理
    function self_handled(){
        $where['Orders.status'] = 2;
        $where['Orders.user_id'] = get_current_admin_id();
        $where['FailuresRepair.building_id'] = getBuilding_id(get_current_admin_id());
        $this->_list($where);
        $this->display('index');
    }

//    获取列表数据
    function _list($other = '')
    {
        $this->get_operator();
        $this->get_campus();
        $this->get_building();
        if (IS_POST) {
            $campus = I('post.campus');
            $building = I('post.building');
            $operator = I('post.operator');
            $start_time = I('post.start_time');
            $end_time = I('post.end_time');
            $name = I('post.name');
            if (!empty($campus) && $campus != 0) {
                $where['campus_id'] = $campus;
            }
            if (!empty($building) && $building != 0) {
                $where['building_id'] = $building;
            }
            if (!empty($operator) && $operator != 0) {
                $where['operator_id'] = $operator;
            }
            if (!empty($start_time) && !empty($end_time)) {
                $where['creation_time'] = array(array('egt', strtotime($start_time)), array('elt', strtotime($end_time)));
            }
            if (!empty($name)) {
                $where['name'] = array('like', '%' . $name . '%');
            }
            $formget = array(
                'name' => $name,
                'start_time' => $start_time,
                'end_time' => $end_time
            );
            $this->assign('formget', $formget);
        }
        if (!empty($other)) {
            $where['_complex'] = $other;
        }
        $count = $this->failures_repair_model->where($where)->count();
        $page = $this->page($count, 20);
        $failures_repair = $this->failures_repair_model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("failures_repair", $failures_repair);
//        dump($failures_repair);
        $this->assign("Page", $page->show('Admin'));
    }

//    搜索
    function search()
    {
        if (IS_POST) {
            $campus = I('post.campus');
            $building = I('post.building');
            $operator = I('post.operator');
            $start_time = I('post.start_time');
            $end_time = I('post.end_time');
            $name = I('post.name');
            if (!empty($campus) && $campus != 0) {
                $where['campus_id'] = $campus;
            }
            if (!empty($building) && $building != 0) {
                $where['building_id'] = $building;
            }
            if (!empty($operator) && $operator != 0) {
                $where['operator_id'] = $operator;
            }
            if (!empty($start_time) && !empty($end_time)) {
                $where['creation_time'] = array(array('egt', strtotime($start_time)), array('elt', strtotime($end_time)));
            }
            if (!empty($name)) {
                $where['name'] = array('like', '%' . $name . '%');
            }
            $formget = array(
                'name' => $name,
                'start_time' => $start_time,
                'end_time' => $end_time
            );
            $this->assign('formget', $formget);
            return $where;
        }
    }

//    获取校区
    function get_campus()
    {
        import("Tree");
        $tree = new \Tree();
        $campus_id = I('campus');
        $campus = $this->campus_model->select();
        foreach ($campus as $r) {
            $r['selected'] = $r['id'] == $campus_id ? 'selected' : '';
            $array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$title</option>";
        $tree->init($array);
        $select_campus = $tree->get_tree(0, $str);

        $this->assign("select_campus", $select_campus);
    }

//    获取运营商
    function get_operator()
    {
        import("Tree");
        $tree = new \Tree();
        $operator_id = I('post.operator');
        $str = "<option value='\$id' \$selected>\$spacer \$title</option>";

        $operator = $this->operator_model->select();
        foreach ($operator as $r) {
            $r['selected'] = $r['id'] == $operator_id ? 'selected' : '';
            $array[] = $r;
        }
        $tree->init($array);
        $select_operator = $tree->get_tree(0, $str);

        $this->assign("select_operator", $select_operator);
    }

    //获取区域
    function get_building()
    {
        $campus_id = I('campus');
        if (!empty($campus_id) && $campus_id != 0) {
            import("Tree");
            $tree = new \Tree();
            $building_id = I('post.building');
            $str = "<option value='\$id' \$selected>\$spacer \$title</option>";

            $building = $this->building_model->where(array('campus_id' => $campus_id))->select();
            foreach ($building as $r) {
                $r['selected'] = $r['id'] == $building_id ? 'selected' : '';
                $array[] = $r;
            }
            $tree->init($array);
            $select_building = $tree->get_tree(0, $str);
            if (IS_AJAX) {
                $this->ajaxReturn(array('data' => $select_building));
            }

            if (IS_POST) {
                $this->assign("select_building", $select_building);
            }
        }
    }

    //报修单详情
    public function details()
    {
        $id = intval(I("get.id"));
        $result = $this->failures_repair_model->where(array('id' => $id))->find();
        $this->assign($result);
//        dump($result);
        $this->display();
    }

    //接单
    public function orders()
    {
        $data['failures_repair_id'] = intval(I('get.id'));
        $data['user_id'] = get_current_admin_id();
        $data['status'] = 1;
        $data['order_time'] = time();
        if ($this->order_model->add($data)) {
            $this->success("接单成功！", U("FailuresRepair/index"));
        } else {
            $this->error("接单失败！");
        }
    }

    //保存反馈
    public function save_comment(){
        $id=intval(I('post.id'));
        $data['admin_comment']=I('post.admin_comment');
        if ($this->order_model->where("id=$id")->save($data)){
            $this->success("保存成功");
        }else{
            $this->error('保存失败');
        }
    }

    public function handle(){
        $id = intval(I("get.id"));
        $result = $this->failures_repair_model->where(array('id' => $id))->find();
        $this->assign($result);
//        dump($result);
        $this->display();
    }

    //处理
    public function save_handle(){
        $id=intval(I('post.id'));
        $data['admin_comment']=I('post.admin_comment');
        $data['status']=2;
        $data['handle_time']=time();
        if ($this->order_model->where("id=$id")->save($data)){
            $this->success("保存成功",U('FailuresRepair/index'));
        }else{
            $this->error('保存失败');
        }
    }

    public function exchange(){
        $id = intval(I("get.id"));
        $result = $this->failures_repair_model->where(array('id' => $id))->find();

        $users=$this->users_model->where("id!=1")->select();
        $this->assign('users',$users);
        $this->assign($result);
        $this->display();
    }

    public function save_exchange(){
        $id=intval(I('post.id'));
        $data['user_id']=I('post.user_id');
        if ($this->order_model->where("id=$id")->save($data)){
            $this->success("转移成功",U('FailuresRepair/index'));
        }else{
            $this->error('转移失败');
        }
    }

    public function delete(){
        $id=intval(I('get.id'));
        $result = $this->failures_repair_m->where(array('id' => $id))->delete();
        if ($result){
            $this->success("删除成功");
        }else{
            $this->error('删除失败');
        }
    }

}